package cn.minimelon.api.controller.module;

import cn.hutool.core.util.StrUtil;
import cn.minimelon.api.controller.common.BaseController;
import cn.minimelon.api.domain.common.AjaxResult;
import cn.minimelon.api.domain.common.PageQuery;
import cn.minimelon.api.domain.common.TableDataInfo;
import cn.minimelon.api.domain.enums.SwTypeEnum;
import cn.minimelon.api.domain.module.dto.ModelTaskData;
import cn.minimelon.api.domain.module.vo.DataModelVO;
import cn.minimelon.api.service.module.IDataModelService;
import cn.minimelon.api.utils.MelonUtils;
import org.noear.solon.annotation.*;
import org.noear.solon.validation.annotation.HttpPart;
import org.noear.solon.validation.annotation.NoRepeatSubmit;

import java.util.Date;
import java.util.List;

/**
 * 模型A配置
 *
 * @author hiro
 */
@Controller
@Mapping("/module/model")
public class DataModelController extends BaseController {
    @Inject
    private IDataModelService modelService;

    /**
     * 获取模型配置列表
     */
    @Post
    @Mapping("/list")
    public TableDataInfo list(PageQuery<DataModelVO> pageQuery) {
        startPage(pageQuery);
        List<DataModelVO> list = modelService.selectList(pageQuery.getQueryParam());
        return getDataTable(list);
    }

    /**
     * 根据模型UID获取详细信息
     */
    @Get
    @Mapping(value = "/one/{uid}")
    public AjaxResult getInfo(String uid) {
        return AjaxResult.success(modelService.selectById(uid));
    }

    /**
     * 根据模型编码获取完全信息
     */
    @Get
    @Mapping(value = "/full/{code}")
    public AjaxResult getFullInfo(String code) {
        return AjaxResult.success(modelService.selectFullInfo(code));
    }

    /**
     * 新增模型配置
     */
    @NoRepeatSubmit(value={HttpPart.body})
    @Post
    @Mapping
    public AjaxResult add(DataModelVO dataModel) {
        String unique = modelService.checkCodeUnique(dataModel);
        if (StrUtil.equals(SwTypeEnum.YES.val(), unique)) {
            return error(StrUtil.format("新增模型{}失败，模型编码已存在", dataModel.getCode()));
        }
        dataModel.setUid(MelonUtils.snowId("M"));
        dataModel.setCreateTime(new Date());
        dataModel.setCreateBy(getUsername());
        if (modelService.insert(dataModel) > 0) {
            return success(dataModel);
        } else {
            return error();
        }
    }

    /**
     * 修改模型配置
     */
    @NoRepeatSubmit(value={HttpPart.body})
    @Put
    @Mapping
    public AjaxResult update(DataModelVO dataModel) {
        String unique = modelService.checkCodeUnique(dataModel);
        if (StrUtil.equals(SwTypeEnum.YES.val(), unique)) {
            return AjaxResult.error(StrUtil.format("新增模型{}失败，模型编码已存在", dataModel.getCode()));
        }
        dataModel.setUpdateBy(getUsername());
        dataModel.setUpdateTime(new Date());
        return toAjax(modelService.update(dataModel));
    }

    /**
     * 删除模型配置
     */
    @Delete
    @Mapping("/{uid}")
    public AjaxResult remove(String uid) {
        modelService.deleteById(uid);
        return success();
    }

    /**
     * 提交模型配置
     */
    @Post
    @Mapping("/submit")
    public AjaxResult submit(List<DataModelVO> list) {
        ModelTaskData taskData = new ModelTaskData();
        taskData.setModelList(list);
        taskData.setUser(getLoginUser());
        String key = modelService.submit(taskData);
        return success(key);
    }

    /**
     * 拒绝模型配置
     */
    @Post
    @Mapping("/reject")
    public AjaxResult reject(List<DataModelVO> list) {
        ModelTaskData taskData = new ModelTaskData();
        taskData.setModelList(list);
        taskData.setUser(getLoginUser());
        String key = modelService.reject(taskData);
        return success(key);
    }

    /**
     * 同意模型配置
     */
    @Post
    @Mapping("/approve")
    public AjaxResult approve(List<DataModelVO> list) {
        ModelTaskData taskData = new ModelTaskData();
        taskData.setModelList(list);
        taskData.setUser(getLoginUser());
        String key = modelService.approve(taskData);
        return success(key);
    }

    /**
     * 发布模型配置
     */
    @Post
    @Mapping("/deploy")
    public AjaxResult deploy(List<DataModelVO> list) {
        ModelTaskData taskData = new ModelTaskData();
        taskData.setModelList(list);
        taskData.setUser(getLoginUser());
        String key = modelService.deploy(taskData);
        return success(key);
    }
}
